package com.ycd.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.ycd.block.UserBlock;
import com.ycd.domain.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@RefreshScope // 动态刷新配置注解
public class UserController {

    @Value("${server.port}")
    private String port;

    @Value("${test.name}")
    private String name;

    /*
     * @Description: 提供一个根据Id返回User对象的接口，给order服务调用
     **/
    @GetMapping("/getById/{id}")
    // @SentinelResource要对哪个接口限流熔断就使用此注解
    // blockHandler-方法当前类调用 -要加BlockException 的异常参数
    // blockHandlerClass-其它类也可以调用-指定降级方法类
    @SentinelResource(value="user-getById", blockHandler = "degradationMethod" ,blockHandlerClass = UserBlock.class)
    public User getById(@PathVariable("id") Long id){

        return new User(id, "2022-08-23", "今天是周二,java学习!，端口号：" + port + "，自定义配置文件：" + name);
    }
    /*// 这里测试当前类进行服务降级
    public User degradationMethod(@PathVariable("id") Long id, BlockException e){
        e.printStackTrace();
        return new User(-1L, "当前类限流服务降级触发！", "当前服务流量过大，请稍后重试！");
    }*/
}